package cn.lb.org.controller;

import cn.lb.annotation.LBPermission;
import cn.lb.org.domain.Department;
import cn.lb.org.query.DepartmentQuery;
import cn.lb.org.service.IDepartmentService;
import cn.lb.basic.util.PageList;
import cn.lb.basic.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
@LBPermission(name = "部门管理",descs = "关于部门的一些操作")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @LBPermission(name = "部门新增修改")
    @PutMapping
    public R addAndsave( @RequestBody Department department){
        System.out.println(department);
        try {
            if(Objects.isNull(department.getId())){
                departmentService.insert(department);
            }else {
                departmentService.update(department);
            }
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }
    @LBPermission(name = "部门删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id")Long id){
        try {
            departmentService.deleteById(id);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }

    @LBPermission(name = "部门通过id查询")
    @GetMapping("/{id}")
    public R selectById(@PathVariable("id")Long id){
        try {
            Department department = departmentService.selectById(id);
            return R.me().setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }

    @LBPermission(name = "部门查询所有")
    @GetMapping
    public R selectAll(){
        try {
            List<Department> departments = departmentService.selectAll();
            return R.me().setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }


    @LBPermission(name = "部门分页与高级查询")
    @PostMapping
    public R page(@RequestBody DepartmentQuery query){
        try {
            PageList<Department> page = departmentService.page(query);
            return R.me().setData(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败"+e.getMessage());
        }
    }
    @LBPermission(name = "部门批量删除")
    @PatchMapping
    public R deletebatch(@RequestBody List<Long> ids){
        try {
           departmentService.deletebatch(ids);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败"+e.getMessage());
        }
    }


    @GetMapping("/tree")
    public R tree(){
        try {
            List<Department> departments = departmentService.trees();
            return R.me().setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }



}
